package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult list(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal>  d = setmealMapper.list(setmealPageQueryDTO);
        return new PageResult(d.getTotal(), d.getResult());
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        // 先查询setmeal中的数据
        SetmealVO setmealVO =  setmealMapper.getSetmealById(id);

        // 查询套餐菜品
        List<SetmealDish> setmealDish = setmealDishMapper.getSetmealDishBySetmealId(setmealVO.getId());
        if (setmealDish != null) {
            setmealVO.setSetmealDishes(setmealDish);
        }

        return setmealVO;
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        // 保存套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        // 批量保存套餐菜品数据
        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 更新套餐的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 先删除套餐菜品再来保存自身的菜品
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        // 更新套餐的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
        }
        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Override
    public void changeStatus(Integer status, Long id) {

        /**
         * 未考虑到起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
         */
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList =  dishMapper.getBySetmealId(id);
            if (dishList != null) {
                dishList.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal build = Setmeal.builder().status(status).id(id).build();
        // 调用更新方法
        setmealMapper.update(build);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        // 启售的商品不能删除
        Integer num = setmealMapper.getStatusSetmealCountByIds(ids, StatusConstant.ENABLE);

        if (num > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        // 先去删除关联的套餐
        setmealDishMapper.batchDeleteBySetmeal(ids);

        // 删除商品
        setmealMapper.batchDelete(ids);
    }
}
